package co.th.bjc.md.helper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.log4j.Logger;

import co.th.bjc.md.common.ConnectDB;
import co.th.bjc.md.model.Login;

public class LoginHelper {

	private static Logger _log = Logger.getLogger(LoginHelper.class);
	
	private static final String VERIFY_LOGIN = "select * from login where username = ? and password = ?"; 
	public static Login verifyLogin(String username, String password) throws Exception{
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		Login login = null;
		ConnectDB connectDB = new ConnectDB();
		try{
			_log.info("SQL : " + VERIFY_LOGIN);
			conn = connectDB.getConnection();
			pstmt = conn.prepareStatement(VERIFY_LOGIN);
			pstmt.setString(1, username);
			pstmt.setString(2, password);
			
			rs = pstmt.executeQuery();
			if(rs.next()){
				login = new Login();
				login.setUsername(rs.getString("username"));
				login.setPassword(rs.getString("password"));
				login.setMemberCode(rs.getBigDecimal("member_code"));
				login.setActive(rs.getString("active"));
			}
		}catch(Exception ex){
			ex.printStackTrace();
			_log.error("Exception : " + ex);
			throw ex;
		}finally{
			if (rs != null) rs.close();
			if (pstmt != null) pstmt.close();
			connectDB.close();
		}
		return login;
	}
}
